<template>
  <div class="container">
    <div class="body">
        <h1>图书后台管理系统</h1>
        <el-form :model="form" label-width="80px" >
            <el-form-item label="账号">
                <el-input  v-model="form.username" ></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input  v-model="form.password" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<style lang="scss">
.container{
    height: 100vh;
    width: 100vw;
    background-image: url('../assets/background.jpg');
    .body{
        background-color: #f3f3f3;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        display: flex;
        flex-direction: column;
        align-items: center;
        border-radius: 20px;
        padding: 30px 50px;
    }
}
</style>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
    }
  },
  methods: {
  submitForm() {
  const xhr = new XMLHttpRequest();
  xhr.open("POST", "http://localhost:3000/api/login");
  xhr.setRequestHeader("Content-Type", "application/json");
  
  xhr.onload = () => {
    if (xhr.status === 200) {
      const response = JSON.parse(xhr.responseText);  
      if (response.success) {
        localStorage.setItem('username', response.username);
        this.$message.success('登录成功');
        this.$router.push('/Home');
      } else {
        console.log('登录失败:', response.message);
        this.$message.error(response.message);
      }
    } else {
      console.error('请求失败:', xhr.statusText);
      this.$message.error('登录失败');
    }
  };
  
  xhr.send(JSON.stringify({
    username: this.form.username,
    password: this.form.password
  }));
}
}
}

</script>
